package com.qms.repast.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qms.repast.R;
import com.qms.repast.entity.reqbean.ReqRemark;
import com.qms.repast.entity.resbean.RemarkBean;
import com.qms.repast.ui.base.BaseActivity;
import com.qms.repast.ui.presenter.OrderRemarkPresenter;
import com.qms.repast.ui.view.IOrderRemarkView;
import com.qms.repast.utils.ToastUtils;
import com.qms.repast.weidgets.common.LabelsView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author 泅渡者
 * @date 2018/10/23 0023
 * @describe: 点餐备注
 */
public class OrderRemarkActivity extends BaseActivity<OrderRemarkPresenter> implements IOrderRemarkView {
	@BindView(R.id.iv_back)
	ImageView ivBack;
	@BindView(R.id.tv_title)
	TextView tvTitle;
	@BindView(R.id.tv_rightBtn)
	TextView tvRightBtn;
	@BindView(R.id.smallLabel)
	LabelsView smallLabel;
	@BindView(R.id.edt_remark)
	EditText edtRemark;

	private String shopId;

	@Override
	protected int initLayout () {
		return R.layout.activity_order_remark;
	}

	@Override
	protected void initPresenter (Intent intent) {
		mPresenter = new OrderRemarkPresenter(this, this);
		shopId = intent.getStringExtra("shopId");
	}

	@Override
	protected void initView () {
		tvTitle.setText("订单备注");
		tvRightBtn.setText("完成");
	}

	@Override
	protected void initData () {
		ReqRemark reqRemark = new ReqRemark();
		reqRemark.setRemarkType("3");
		reqRemark.setShopId(shopId);
		mPresenter.getRemarkList(reqRemark);

		//标签的点击监听
		smallLabel.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
			@Override
			public void onLabelClick (TextView label, Object data, int position) {
				RemarkBean.BodyBean remark = (RemarkBean.BodyBean) data;
				String str = edtRemark.getText().toString().trim();
				if ((str.length() + remark.getRemarkName().length() > 50)) {
					ToastUtils.showToast("文本超出限制。");
					return;
				}
				if (!isContains(str, remark.getRemarkName())) {
					if (TextUtils.isEmpty(str)) {
						edtRemark.setText(remark.getRemarkName() + "");
					} else {
						edtRemark.setText(str + "，" + remark.getRemarkName());
					}
				}
				edtRemark.setSelection(edtRemark.getText().toString().length());
			}
		});
	}

	@OnClick({R.id.iv_back, R.id.tv_rightBtn})
	public void onViewClicked (View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				break;
			case R.id.tv_rightBtn:
				break;
			default:
				break;
		}
	}


	private boolean isContains (String target, String conditions) {
		if (target.indexOf(conditions) == -1) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void updateLables (List<RemarkBean.BodyBean> remarks) {
		smallLabel.setLabels(remarks, new LabelsView.LabelTextProvider<RemarkBean.BodyBean>() {
			@Override
			public CharSequence getLabelText (TextView label, int position, RemarkBean.BodyBean data) {
				//根据data和position返回label需要显示的数据。
				return data.getRemarkName();
			}
		});
	}
}
